<?php

// 商户钱包

namespace app\api\model;

use think\Model;

class PaymentCash extends Model
{

    // 数据表名称,不包括表前缀
    protected $name = 'payment_cash';
    // 数据表主键
    protected $pk = 'merchant_id';
    // 时间字段取出后的默认时间格式
    protected $dateFormat = false;

    /**
     * 更新商户钱包
     * @access public
     * @param  array  $recharge                订单数组
     * @param  array  $merchant_value          商户配置值
     * @param  array  $children_company_value  子公司配置值
     * @return mixed
     */
    public static function addCash($recharge = [], $merchant_value = [], $children_company_value = [])
    {
        try {
            // 结算周期是DO(实时结算)
            if ($merchant_value['settlement_type'] == 1 && $merchant_value['settlement_day'] == 0) {

                $payment_type_rate = $merchant_value['payment_type_rate'];

                if (isset($payment_type_rate[$recharge['type']])) {
                    $type_rate = $payment_type_rate[$recharge['type']];
                } else {
                    $type_rate = 0;
                    payment_error_log(610001, $recharge, $merchant_value);
                }

                // 开启通道费率结算
                if ($merchant_value['company_rate_open'] == 1) {

                    $company_payment_type_rate = $children_company_value['payment_type_rate'];

                    if (isset($company_payment_type_rate[$recharge['type']])) {
                        $company_type_rate = $company_payment_type_rate[$recharge['type']];
                    } else {
                        $company_type_rate = 0;
                        payment_error_log(610002, $recharge, $children_company_value);
                    }
                    $type_rate = $type_rate + $company_type_rate;
                }

                $rate = 1 - $type_rate;
                $cash = ceil(strval($recharge['cash'] * $rate));

                $merchant_data = self::where(['merchant_id' => $recharge['merchant_id']])->find();
                if ($merchant_data) {
                    // 更新商户钱包金额
                    try {
                        self::where(['merchant_id' => $recharge['merchant_id']])
                            ->inc('cash', $cash)
                            ->inc('sum_cash', $cash)
                            ->inc('count', 1)
                            ->update(['update_time' => time()]);
                    } catch (\Exception $e) {
                        payment_error_log(610003, $recharge, $e->getMessage());
                    }
                } else {
                    $data['merchant_id'] = $recharge['merchant_id'];    // 商户号
                    $data['cash'] = $cash;                              // 商户现金余额
                    $data['sum_cash'] = $cash;                          // 商户总现金
                    $data['count'] = 1;                                 // 来源总次数
                    $data['freeze_cash'] = 0;                           // 冻结金额
                    $data['transit_cash'] = 0;                          // 过渡金额
                    $data['paid_cash'] = 0;                             // 代付余额
                    $data['paid_sum_cash'] = 0;                         // 商户代付总金额
                    $data['paid_count'] = 0;                            // 代付总次数
                    $data['withdrawal_sum_cash'] = 0;                   // 商户提现总金额
                    $data['withdrawal_count'] = 0;                      // 提现总次数
                    $data['status'] = 1;                                // 状态
                    $data['dates'] = date('Y-m-d H:i:s');               // 统计时间
                    $data['create_time'] = time();                      // 创建时间
                    $data['update_time'] = time();                      // 更新时间
                    // 添加商户钱包金额
                    try {
                        self::create($data);
                    } catch (\Exception $e) {
                        payment_error_log(610004, $recharge, $e->getMessage());
                    }
                }
            }
        } catch (\Exception $e) {
            payment_error_log(610000, $recharge, $e->getMessage());
        }
    }
}
